/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ftpcombiner.backup;

import ftpcombiner.server.serverconnection.Folder;
import ftpcombiner.server.serverconnection.ServerItem;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

/**
 *
 * @author Ben
 */
public class BackupThread extends Thread {

    private static BackupThread instance;
    private static ArrayList<File> backupFiles;
    private String basePath;

    public BackupThread() {
        super("BackupThread");
    }

    public static void backup(File config) throws FileNotFoundException {
        if (instance == null) {
            backupFiles = new ArrayList<>();
            if (config.isDirectory()) {
                backupFiles.add(config);
            } else {
                BufferedReader fileReader = new BufferedReader(new FileReader(config));
                String fileString = "";
                while (fileString != null) {
                    try {
                        fileString = fileReader.readLine();
                    } catch (IOException ex) {
                        System.err.println("Cannot read config file");
                    }
                    if (fileString == null) {
                        break;
                    }
                    backupFiles.add(new File(fileString));
                }
            }
            for (File file : backupFiles) {
                System.out.println("backup file: " + file);
            }
            instance = new BackupThread();
            instance.start();
        } else {
            System.err.println("Backup already in progress");
        }
    }

    @Override
    public void run() {
        System.out.println("Backup started");
        for (File folder : backupFiles) {
            for (File f : folder.listFiles()) {
                System.out.println(f.getPath());
                System.out.println("======");
                copyToServer(f);
                System.out.println("Deleting Files");

            }
        }
        for (File folder : backupFiles) {
            basePath = folder.getPath();
            for (ServerItem item : TransactionManager.getInstance().getCurrentFiles()) {
                deleteFiles(item);
            }
        }
        instance = null;
        backupFiles = null;
        System.out.println("Backup finished");
        System.exit(0);
    }

    private void copyToServer(File file) {
        System.out.println("CurDir: " + TransactionManager.getInstance().getCurrentDir());
        System.out.println("Copying " + file.getName());
        if (file.isDirectory()) {
            ServerItem[] items = TransactionManager.getInstance().getCurrentFiles();
            Arrays.sort(items);
            if (Arrays.binarySearch(items, file.getName()) < 0) {
                TransactionManager.getInstance().createDirectory(file.getName());
            }
            TransactionManager.getInstance().changeDirectory(file.getName());
            for (File f : file.listFiles()) {
                copyToServer(f);
//                File[] files = file.listFiles();
//                Arrays.sort(files);
//                for(ServerItem item:TransactionManager.getMainInstance().getCurrentFiles()){
//                    if(Arrays.binarySearch(files, item.getName())<0){
//                        TransactionManager.getMainInstance().delete((ftpcombiner.server.serverconnection.File)item);
//                    }
//                }
            }
            TransactionManager.getInstance().changeDirectoryUp();
        } else {
            TransactionManager.getInstance().upload(file);
        }

    }

    public void deleteFiles(ServerItem folder) {
        if (folder instanceof Folder) {
            if (folder instanceof Folder) {
                TransactionManager.getInstance().changeDirectory(folder.getName());
            }
            String curDir = TransactionManager.getInstance().getCurrentDir();
            String dir = basePath + curDir.replace("/", "\\");
            System.out.println(dir);
            File[] localFiles = (new File(dir)).listFiles();
            System.out.println("CurDir del: " + curDir);
            ServerItem[] remoteFiles = TransactionManager.getInstance().getCurrentFiles();
            for (ServerItem item : remoteFiles) {
                boolean found = false;
                for (File file : localFiles) {
                    System.out.println("local: " + file.getName() + " remote: " + item.getName());
                    if (file.getName().equals(item.getName())) {

                        found = true;
                    }
                }
                if (item instanceof Folder) {
                    deleteFiles(item);
                    if (!found) {
                        System.out.println("Deleting " + item.getName());
                        TransactionManager.getInstance().deleteDirectory((Folder) item);
                    }
                } else {
                    if (!found) {
                        System.out.println("Deleting " + item.getName());
                        TransactionManager.getInstance().delete((ftpcombiner.server.serverconnection.File) item);
                    }
                }
            }
            TransactionManager.getInstance().changeDirectoryUp();
        } else {
            TransactionManager.getInstance().delete((ftpcombiner.server.serverconnection.File) folder);
        }
    }
}
